// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Diagnostics;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// An object that formats soap message return values.  Used by the 
	/// <see cref="SoapMessageFormatter"/> class.
	/// </summary>
	public class ReturnValueFormatter
	{
		/// <summary>
		/// The web service return value which contains information used
		/// to format the return value's data.
		/// </summary>
		WebServiceReturnValue m_ReturnValue;

		/// <summary>
		/// Creates a new instance of the <see cref="ReturnValueFormatter"/>
		/// class.
		/// </summary>
		public ReturnValueFormatter( )
		{
		}
		
		/// <summary>
		/// Creates a new instance of the <see cref="ReturnValueFormatter"/>.
		/// </summary>
		/// <param name="returnValue">The web service return value that
		/// will be used to format the return value.</param>
		public ReturnValueFormatter( WebServiceReturnValue returnValue )
		{
			m_ReturnValue = returnValue;
		}

		/// <summary>
		/// Formats a return value name and value pair.
		/// </summary>
		/// <param name="index">The return value index.</param>
		/// <param name="name">The name of the return value.</param>
		/// <param name="valueText">The return value's value.</param>
		/// <returns>A formatted return value name-value pair string.</returns>
		public string Format( int index, string name, string valueText )
		{
			bool DisplayReturnValue = true;
			
			// Find the corresponding return value.

			
			WebServiceArrayItem ArrayItem = null;
	
			if( m_ReturnValue != null )
			{
				ArrayItem = GetReturnValue( m_ReturnValue, index );
			}
			
			// Get the formatted value.

			string FormattedValue = valueText;

			if( ArrayItem != null )
			{
				DisplayReturnValue = ArrayItem.Display;
				
				if( DisplayReturnValue )
				{
					FormattedValue = ValueFormatter.Format(
						ArrayItem.ItemType,
						valueText );
				}
			}
			else if( m_ReturnValue != null )
			{					
				DisplayReturnValue = m_ReturnValue.Display;
				
				if( DisplayReturnValue )
				{
					string ItemType = m_ReturnValue.ParameterType;
					
					if( m_ReturnValue.IsArray )
					{
						ItemType = m_ReturnValue.ArrayType;
					}
					
					FormattedValue = ValueFormatter.Format( 
					    ItemType,
						valueText );
				}
			}			

			// Format the name-value pair.

			string FormattedText = String.Empty;
			
			if( DisplayReturnValue )
			{
				FormattedText = FormatNameValuePair( name, FormattedValue );
			}
			
			return FormattedText;
		}
		
		/// <summary>
		/// Gets the web service array item at the specified index.
		/// </summary>
		/// <param name="returnValue">A return value.</param>
		/// <param name="index">The index into the return value array.</param>
		/// <returns>A web service array item if the index is valid otherwise
		/// returns <see langword="null"/>.</returns>
		private static WebServiceArrayItem GetReturnValue( WebServiceReturnValue returnValue,
			int index )
		{
			WebServiceArrayItem Item = null;

			if( returnValue.Items.Count > index )
			{
				Item = returnValue.Items[ index ];
			}
			return Item;
		}		

		/// <summary>
		/// Formats a name value pair.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="valueText">The value.</param>
		/// <returns>A formatted name value pair.</returns>
		private static string FormatNameValuePair( string name, string valueText )
		{
			return String.Concat( name, ": ", 
				valueText, "\r\n" );
		}
	}
}

